using UnityEngine;
using Framework.Singleton;

namespace Framework.Util
{
    public class GameObjectUtil : Singleton<GameObjectUtil>
    {
        private GameObjectUtil() { }

        #region Find
        /// <summary>查找GameObject（全局路径）</summary>
        /// <param name="pPath">全局路径</param>
        public GameObject FindGameObject(string pPath)
        {
            GameObject findGameObject = GameObject.Find(pPath);
            return findGameObject;
        }

        /// <summary>查找创建GameObject（全局路径）</summary>
        /// <param name="pPath">全局路径</param>
        /// <returns>GameObject</returns>
        public GameObject FindOrCreateGameObject(string pPath)
        {
            GameObject findGameObject = GameObject.Find(pPath);
            if (findGameObject != null)
                return findGameObject;
            else
                return CreateGameObject(pPath);
        }

        /// <summary>查找Transform（全局路径）</summary>
        /// <param name="pPath">全局路径</param>
        public Transform FindTransform(string pPath)
        {
            GameObject findGameObject = GameObject.Find(pPath);
            return findGameObject?.transform;
        }

        /// <summary>查找子对象</summary>
        /// <param name="pParentGameObject">父对象</param>
        /// <param name="pPath">父节点路径</param>
        public GameObject FindChild(GameObject pParentGameObject, string pPath)
        {
            Transform childTransform = pParentGameObject.transform.Find(pPath);
            return childTransform?.gameObject;
        }

        /// <summary>查找创建子对象</summary>
        /// <param name="pParentGameObject">父对象</param>
        /// <param name="pPath">父节点路径</param>
        public GameObject FindOrCraeteChild(GameObject pParentGameObject, string pPath)
        {
            Transform childTransform = pParentGameObject.transform.Find(pPath);
            if(childTransform != null)
                return childTransform.gameObject;
            else
                return CreateGameObject(pParentGameObject, pPath);
        }
        #endregion

        #region Create
        /// <summary>创建对象</summary>
        /// <param name="pParentGameObject">父对象</param>
        /// <param name="pPath">父节点路径</param>
        /// <returns></returns>
        public GameObject CreateGameObject(GameObject pParentGameObject, string pPath)
        {
            GameObject newGameObject = null;
            if (!string.IsNullOrEmpty(pPath))
            {
                Transform findTransform = pParentGameObject?.transform;
                string[] pathNameArray = pPath.Split('/');
                foreach (string pathName in pathNameArray)
                {
                    if (findTransform == null)
                    {
                        GameObject childGameObject = GameObject.Find(pathName);
                        if (childGameObject == null)
                            childGameObject = new GameObject(pathName);

                        findTransform = childGameObject.transform;
                    }
                    else
                    {
                        Transform childTransform = findTransform.Find(pathName);
                        if (childTransform == null)
                        {
                            childTransform = new GameObject(pathName).transform;
                            childTransform.SetParent(findTransform);
                        }

                        findTransform = childTransform;
                    }
                }

                newGameObject = findTransform.gameObject;
            }

            return newGameObject;
        }

        /// <summary>创建对象（根节点创建）</summary>
		/// <param name="pPath">路径</param>
		public GameObject CreateGameObject(string pPath)
        {
            return CreateGameObject(null, pPath);
        }
        #endregion

        #region GetComponent
        /// <summary>获取Component（全局路径）</summary>
        /// <typeparam name="T">Component类型</typeparam>
        /// <param name="pPath">全局路径</param>
        public T GetComponent<T>(string pPath)
        {
            GameObject obj = FindGameObject(pPath);
            if (obj != null)
                return obj.GetComponent<T>();
            else
                return default;
        }

        /// <summary>获取Component</summary>
        /// <typeparam name="T">Component类型</typeparam>
        /// <param name="pParentGameObject">父对象</param>
        /// <param name="pPath">父对象路径</param>
        /// <returns></returns>
        public T GetComponent<T>(GameObject pParentGameObject, string pPath)
        {
            GameObject childGameObject = FindChild(pParentGameObject, pPath);
            if (childGameObject)
                return childGameObject.GetComponent<T>();
            else
                return default;
        }

        #endregion
    }
}